<?php
	session_start();
	require_once("../includes/public.inc.php");
	$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	$type = isset($_GET['type'])?$_GET['type']:'';
	$id = isset($_GET['id'])?$_GET['id']:0;
	$type =  isset($_GET['type'])?$_GET['type']:'';
	$act = isset($_GET['act'])?$_GET['act']:'';
	$gitfname = isset($_POST['productname'])?$_POST['productname']:'';
	$getway = isset($_POST['productlx'])?$_POST['productlx']:'';
	$qty = isset($_POST['txtQty'])?$_POST['txtQty']:'';
	//print_r($_POST);exit;
	
	if ($act=='dui')
	{
		if(empty($username))
		{
			echo "<script>alert('亲你还没有登录领取哦哦');history.back();</script>";	
			exit;
		}
		else 
		{
			if ($getway&&$gitfname&&$qty)
			{
				$gift = mysql_fetch_array(mysql_query("select * from ".DBQIAN."gift where name='".$gitfname."' order by id desc limit 1"));
				$user = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where username='".$username."' order by id desc limit 1"));
				if (!$gift)
				{
					echo "<script>alert('产品不存在');history.back();</script>";	
					exit;
				}
				else
				{
					if ($user&&$user['credit'] > ($qty*$gift['credit']))
					{
						//echo 123;exit;
						
						$in_exchange = mysql_query("insert into ".DBQIAN."exchange (credit,time,username,giftname,qty,post_id,usercredit,nowcredit,status,type) values(".($qty*$gift['credit']).",'".date('Y-m-d H:i:s',time())."','".$username."','".$gitfname."',".$qty.",".$getway.",".$user['credit'].",".($user['credit']-($qty*$gift['credit'])).",0,".$gift['type'].")");
						$up_user = mysql_query("update ".DBQIAN."users set credit=".($user['credit']-($qty*$gift['credit']))." where username='".$username."'");
						//echo "insert into ".DBQIAN."exchange (credit,time,username,giftname,qty,post_id,usercredit,nowcredit,status) values(".($qty*$gift['credit']).",'".date('Y-m-d H:i:s',time())."','".$username."','".$gitfname."',".$qty.",".$getway.",".$user['credit'].",".($user['credit']-($qty*$gift['credit'])).",0";exit;
						if ($up_user&&$in_exchange)
						{
							echo "<script>alert('您的兑换申请已成功提交！感谢您的参与。');window.location.href = 'index.php?type=".$type."';</script>";exit;
						}
					}
					else 
					{
						echo "<script>alert('你的积分不足！');history.back();</script>";	
						exit;
					}
				}
			}
			else 
			{
				echo "<script>alert('提交的参数错误！');history.back();</script>";	
				exit;
			}
		}
	}